﻿using Panuon.WPF;
using Panuon.WPF.UI;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Input;

namespace Panuon_FileDelete.ViewModels
{
    public class MainViewModel : NotifyPropertyChangedBase
    {
        public MainViewModel()
        {
            _BegTime = DateTime.Parse(DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd 00:00:00"));
            _EndTime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 23:59:59")); ;
        }
        #region 属性

        private string _FilePath;
        /// <summary>
        /// 选择的文件路径
        /// </summary>
        public string FilePath { get { return _FilePath; } set { Set(ref _FilePath, value); } }

        private List<string> _FileType = new List<string>();
        /// <summary>
        /// 文件类型
        /// </summary>
        public List<string> FileType { get { return _FileType; } set { Set(ref _FileType, value); } }


        private DateTime _BegTime;
        /// <summary>
        /// 文件创建开始时间
        /// </summary>
        public DateTime BegTime { get { return _BegTime; } set { Set(ref _BegTime, value); } }

        private DateTime _EndTime;
        /// <summary>
        /// 文件创建结束时间
        /// </summary>
        public DateTime EndTime { get { return _EndTime; } set { Set(ref _EndTime, value); } }

        private bool _IsLoad = false;
        /// <summary>
        /// 是否显示正在加载
        /// </summary>
        public bool IsLoad { get { return _IsLoad; } set { Set(ref _IsLoad, value); IsEnable = !_IsLoad; } }

        public bool _IsEnable = true;
        /// <summary>
        /// 页面元素是否可点击
        /// </summary>
        public bool IsEnable { get { return _IsEnable; } set { Set(ref _IsEnable, value); } }

        #endregion

        #region 计算相关属性

        private long _TotalDir = 0;
        /// <summary>
        /// 扫描的文件夹
        /// </summary>
        public long TotalDir { get { return _TotalDir; } set { Set(ref _TotalDir, value); } }

        private long _TotalFiles = 0;
        /// <summary>
        /// 扫描的文件
        /// </summary>
        public long TotalFiles { get { return _TotalFiles; } set { Set(ref _TotalFiles, value); } }

        private long _DeleteFiles = 0;
        /// <summary>
        /// 删除的文件
        /// </summary>
        public long DeleteFiles { get { return _DeleteFiles; } set { Set(ref _DeleteFiles, value); } }

        private decimal _DeleteFileStorage = 0m;
        /// <summary>
        /// 删除的文件
        /// </summary>
        public decimal DeleteFileStorage { get { return _DeleteFileStorage; } set { Set(ref _DeleteFileStorage, value); } }

        private long _DeleteDir = 0;
        /// <summary>
        /// 删除的文件夹数
        /// </summary>
        public long DeleteDir { get { return _DeleteDir; } set { Set(ref _DeleteDir, value); } }

        #endregion

        #region 事件

        /// <summary>
        /// 开始执行删除操作
        /// </summary>
        public ICommand Command_Start
        {
            get
            {
                return new RelayCommand((e) =>
                {
                    if (string.IsNullOrEmpty(this.FilePath))
                    {
                        MessageBox.Show("请选择文件夹", "提示", MessageBoxButtons.OK);
                    }
                    else if (!Directory.Exists(this.FilePath))
                    {
                        MessageBox.Show("所选文件夹不存在", "提示", MessageBoxButtons.OK);
                    }
                    else if (this.FileType.Count == 0)
                    {
                        MessageBox.Show("请选择文件类型", "提示", MessageBoxButtons.OK);
                    }
                    else
                    {
                        this.IsLoad = true;

                        DateTime temp_BegTime = this.BegTime;
                        DateTime temp_EndTime = this.EndTime;

                        Task.Factory.StartNew(() =>
                        {
                            this.List_Ex = this.FileType;

                            this.InitTotla();

                            ScanDir(this.FilePath, temp_BegTime, temp_EndTime);

                            this.IsLoad = false;

                            MessageBox.Show("操作完成", "提示", MessageBoxButtons.OK);
                        });
                    }
                });
            }
        }

        /// <summary>
        /// 初始化统计信息
        /// </summary>
        private void InitTotla()
        {
            this.TotalDir = 0;
            this.TotalFiles = 0;
            this.DeleteDir = 0;
            this.DeleteFiles = 0;
            this.DeleteFileStorage = 0;
        }

        // 记录要处理的文件后缀
        public List<string> List_Ex = new List<string>();

        /// <summary>
        /// 扫描文件和文件夹的递归方法
        /// </summary>
        /// <param name="path"></param>
        private void ScanDir(string path, DateTime Beg, DateTime End)
        {
            if (this.IsLoad)
            {
                DirectoryInfo rootDir = new DirectoryInfo(path);

                this.TotalDir++; // 扫描的文件夹 +1 

                // 所有文件
                FileInfo[] childFiles = rootDir.GetFiles();

                foreach (FileInfo file in childFiles)
                {
                    this.TotalFiles++; // 扫描的文件 + 1

                    Console.WriteLine(this.FileType);

                    if (BegTime < file.CreationTime && file.CreationTime <= EndTime && this.List_Ex.Contains(file.Extension))
                    {
                        this.DeleteFileStorage += file.Length;

                        file.Delete();

                        Thread.Sleep(10);
                        // 删除操作
                        this.DeleteFiles++;
                    }
                }

                // 所有文件夹
                DirectoryInfo[] childDir = rootDir.GetDirectories();

                foreach (DirectoryInfo Dic in childDir)
                {
                    ScanDir(Dic.FullName, Beg, End);
                }

                if (this.List_Ex.Contains("空文件夹"))
                {
                    if (rootDir.GetFiles().Length == 0 && rootDir.GetDirectories().Length == 0)
                    {
                        rootDir.Delete();
                        this.DeleteDir++;
                        Thread.Sleep(10);
                    }
                }

                Thread.Sleep(10);
            }
            else 
            {
                return;
            }
        }

        public ICommand Command_Stop
        {
            get
            {
                return new RelayCommand(e =>
                {
                    this.IsLoad = false;
                });
            }
        }

        #endregion
    }
}
